<template>
  <div>
    <el-dialog
      title="卡片设置"
      :visible.sync="open"
      width="800px"
      append-to-body
      @close="handleClose"
    >
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="卡片标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="请输入卡片标题"
            clearable
            @blur="handleQuery"
          />
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
      </el-row>
      <el-table v-loading="loading" :data="listData">
        <el-table-column label="标题" align="center" prop="title" width="200" />
        <el-table-column label="名称" align="center" prop="name" width="200" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="delOpenCard(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加或修改协议对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="400px"
      append-to-body
      @close="visible=false"
    >
      <el-form ref="form" :model="form" label-width="90px" :rules="rules" >
        <el-form-item label="卡片标题" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="卡片名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="visible=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFloorCard,addFloorCard,editFloorCard,delFloorCard } from "@/api/mallAdmin/floorSet/floorSet";
export default {
  props: ["floorId", "cardOpen",'clientType'],
  watch: {
    cardOpen(val) {
      console.log(val);
      this.open = val;
      if (val) {
        this.getList();
      }
    },
    open(val) {
      if (!val) {
        this.$emit("handleClose", false);
      }
    },
    visible(val){
        if(!val){
            this.form = {name:'',title:''}
            this.resetForm("form");
        }
    }
  },
  name: "CardDialog",
  data() {
    return {
      listData: [],
      open: false,
      visible: false,
      loading: false,
      title: "新增",
      form: {
        title: "",
        name: "",
      },
      // 表单校验
      rules: {
        title: [
            { required: true, message: "卡片标题不能为空", trigger: "blur" }
          ],
          name: [
            { required: true, message: "卡片名称不能为空", trigger: "blur" }
          ]
        },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        title: undefined,
        createBy: undefined,
        classify: undefined,
        clientType: this.clientType,
      },
    };
  },

  methods: {
    handleClose() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      listFloorCard({
        ...this.queryParams,
        ...{
          floorId: this.floorId,
        },
      }).then((response) => {
        this.listData = response.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },
    handleAdd(){
        this.title = '新增卡片'
        this.visible = true
    },
    handleUpdate(row){
        this.title = '编辑卡片'
        this.form =JSON.parse(JSON.stringify(row));
        this.visible = true
    },
    delOpenCard(row){
        delFloorCard(row.id).then(res=>{
            this.$modal.msgSuccess("删除成功");
                        this.getList();
        })
    },
    submitForm(){
        this.$refs.form.validate(valid=>{
            if(valid){
                if(this.form.id){
                    editFloorCard({
                    ...this.form,
                    ...{
                        floorId:this.floorId,
                        clientType:this.queryParams.clientType
                    }
                    }).then((res)=>{
                        this.$modal.msgSuccess("修改成功");
                        this.getList();
                        this.visible = false
                    })
                } else {
                    addFloorCard({
                    ...this.form,
                    ...{
                        floorId:this.floorId,
                        clientType:this.queryParams.clientType
                    }
                    }).then((res)=>{
                        this.$modal.msgSuccess("新增成功");
                        this.getList();
                        this.visible = false
                    })
                }
                
            }
        })
    }
  },
};
</script>

<style lang="scss" scoped>
</style>